package com.gao.exception;

/**
 * @author gao
 * @time 2022/11/25 11:37:30
 */
public class OrderException extends RuntimeException {

    private ErrorCode errorCode;
    private Object[] datas = new Object[3];

    public enum ErrorCode {
        OK, REMOTE_PRODUCT_FOUND_FAILURE, REMOTE_PRODUCT_DEDUCT_STOCK_FAILURE,
        ORDER_NOT_FOUND
    }

    public OrderException() {
    }

    public OrderException(String message) {
        super(message);
    }

    public OrderException(Throwable cause) {
        super(cause);
    }

    public OrderException(ErrorCode errorCode, Integer expectedPid) {
        this.errorCode = errorCode;
        this.datas[0] = expectedPid;
    }

    @Override
    public String getMessage() {
        switch (errorCode) {
            case OK:
                return "Should not get here.";
            case REMOTE_PRODUCT_FOUND_FAILURE:
                return String.format("Remote product found failure: %d", datas[0]);
            case REMOTE_PRODUCT_DEDUCT_STOCK_FAILURE:
                return String.format("Remote product deduct stock failure, %d", datas[0]);
            case ORDER_NOT_FOUND:
                return String.format("Order not found: %d", datas[0]);
            default:
        }
        return "";
    }
}
